package site.lizhivscaomei.aliyun.oss.config;

import com.aliyun.oss.OSSClient;
import site.lizhivscaomei.aliyun.oss.service.ClientDirectUploadService;
import site.lizhivscaomei.aliyun.oss.service.impl.DefaultClientDirectUploadServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云OSS自动配置类
 * Created by lizhi on 2019/1/10.
 */
@Configuration
@ConditionalOnClass(ClientDirectUploadService.class)
@ConditionalOnProperty(prefix = "oss.aliyun", value = "enabled", matchIfMissing = true)
public class AliyunOssAutoConfig {
    @Autowired
    OSSConfigProperties ossConfigProperties;

    @Bean
    @ConditionalOnMissingBean(OSSClient.class)
    public OSSClient ossClient() {
        return new OSSClient(ossConfigProperties.getEndpoint(), ossConfigProperties.getAccessId(), ossConfigProperties.getAccessKey());
    }

    @Bean
    @ConditionalOnMissingBean(ClientDirectUploadService.class)
    public ClientDirectUploadService clientDirectUploadService() {
        return new DefaultClientDirectUploadServiceImpl(ossConfigProperties,ossClient());
    }

}
